<?php

namespace Modules\Admin\Http\Controllers;

use App\Admin;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Admin\Http\Requests\AddAdminRequest;
use Spatie\Permission\Models\Role;

class AdminUserController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        $admins = Admin::where('name','!=','admin')->paginate(10);
        return view('admin::adminuser.index',compact('admins'));
    }

    /**
     * Show the form for creating a new resource.
     * @return Response
     */
    public function create()
    {
        return view('admin::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function store(AddAdminRequest $request)
    {
        $post = $request->all();
        $post['password'] = bcrypt($post['password']);
        $res = Admin::create($post);
//        dd($res->toArray());
        if($res){
            session()->flash('success','添加成功');
        }else{
            session()->flash('danger','添加失败');
        }
        return redirect()->route('adminuser.index');
    }

    /**
     * Show the specified resource.
     * @return Response
     */
    public function show()
    {
        return view('admin::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @return Response
     */
    public function edit()
    {
        return view('admin::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param  Request $request
     * @return Response
     */
    public function update(AddAdminRequest $request,$id)
    {

        $admin = Admin::find($id);
        $admin->name = $request->input('name');
        $admin->nickname = $request->input('nickname');
        if($request->input('password')){
            $admin->password = bcrypt($request->input('password'));
        }
        $admin->save();
        session()->flash('success','编辑成功');
        return redirect()->route('adminuser.index');

    }

    /**
     * Remove the specified resource from storage.
     * @return Response
     */
    public function destroy($id)
    {
        Admin::destroy($id);
        session()->flash('success','删除成功');
        return back();
    }

    /**
     * 加载为用户分配角色模板
     * @param $adminid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function showRole($adminid){
        $admin = Admin::find($adminid);
        $roles = Role::where('name','!=','master')->get();
        return view('admin::adminuser.role',compact('admin','roles'));
    }
    //处理用户分配角色
    public function assignRole(Request $request,$adminid){
//        dd($adminid);
        $admin = Admin::find($adminid);
        $admin->syncRoles($request->input('name'));
        session()->flash('success','分配角色成功');
        return redirect()->route('adminuser.index');
    }
}
